package com.a16.yuejian.yuejian.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.a16.yuejian.yuejian.R;
import com.a16.yuejian.yuejian.fragment.ClubFragment;
import com.a16.yuejian.yuejian.fragment.HotFragment;
import com.a16.yuejian.yuejian.fragment.MyFragment;
import com.a16.yuejian.yuejian.fragment.PostFragment;
import com.a16.yuejian.yuejian.fragment.TrainFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private LinearLayout mTabTrain;
    private LinearLayout mTabClub;
    private LinearLayout mTabHot;
    private LinearLayout mTabPost;
    private LinearLayout mTabMy;

    private ImageButton mImgTrain;
    private ImageButton mImgClub;
    private ImageButton mImgHot;
    private ImageButton mImgPost;
    private ImageButton mImgMy;

    private TextView mTextTrain;
    private TextView mTextClub;
    private TextView mTextPost;
    private TextView mTextMy;

    private Fragment mTab01;
    private Fragment mTab02;
    private Fragment mTab03;
    private Fragment mTab04;
    private Fragment mTab05;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
        setSelect(2);
    }

    public void setSelect(int i) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        switch (i)
        {
            case 0:
                if (mTab01 == null)
                {
                    mTab01 = new TrainFragment();
                    transaction.add(R.id.id_content, mTab01);
                } else
                {
                    transaction.show(mTab01);
                }
                mImgTrain.setImageResource(R.drawable.ic_train);
                mTextTrain.setTextColor(Color.parseColor("#FCCA00"));
                break;
            case 1:
                if (mTab02 == null)
                {
                    mTab02 = new ClubFragment();
                    transaction.add(R.id.id_content, mTab02);
                } else
                {
                    transaction.show(mTab02);

                }
                mImgClub.setImageResource(R.drawable.ic_club);
                mTextClub.setTextColor(Color.parseColor("#FCCA00"));
                break;
            case 2:
                if (mTab03 == null)
                {
                    mTab03 = new HotFragment();
                    transaction.add(R.id.id_content, mTab03);
                } else
                {
                    transaction.show(mTab03);
                }
                mImgHot.setImageResource(R.drawable.ic_hot);
                break;
            case 3:
                if (mTab04 == null)
                {
                    mTab04 = new PostFragment();
                    transaction.add(R.id.id_content, mTab04);
                } else
                {
                    transaction.show(mTab04);
                }
                mImgPost.setImageResource(R.drawable.ic_post);
                mTextPost.setTextColor(Color.parseColor("#FCCA00"));
                break;
            case 4:
                if (mTab05 == null)
                {
                    mTab05 = new MyFragment();
                    transaction.add(R.id.id_content, mTab05);
                } else
                {
                    transaction.show(mTab05);
                }
                mImgMy.setImageResource(R.drawable.ic_my);
                mTextMy.setTextColor(Color.parseColor("#FCCA00"));
                break;

            default:
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (mTab01 != null)
        {
            transaction.hide(mTab01);
        }
        if (mTab02 != null)
        {
            transaction.hide(mTab02);
        }
        if (mTab03 != null)
        {
            transaction.hide(mTab03);
        }
        if (mTab04 != null)
        {
            transaction.hide(mTab04);
        }
        if (mTab05 != null)
        {
            transaction.hide(mTab05);
        }
    }

    private void initEvent() {
        mTabTrain.setOnClickListener(this);
        mTabClub.setOnClickListener(this);
        mTabHot.setOnClickListener(this);
        mTabPost.setOnClickListener(this);
        mTabMy.setOnClickListener(this);
    }

    private void initView() {
        mTabTrain = (LinearLayout) findViewById(R.id.id_tab_train);
        mTabClub = (LinearLayout) findViewById(R.id.id_tab_club);
        mTabHot = (LinearLayout) findViewById(R.id.id_tab_hot);
        mTabPost = (LinearLayout) findViewById(R.id.id_tab_post);
        mTabMy = (LinearLayout) findViewById(R.id.id_tab_my);

        mImgTrain = (ImageButton) findViewById(R.id.id_tab_train_img);
        mImgClub = (ImageButton) findViewById(R.id.id_tab_club_img);
        mImgHot = (ImageButton) findViewById(R.id.id_tab_hot_img);
        mImgPost = (ImageButton) findViewById(R.id.id_tab_post_img);
        mImgMy = (ImageButton) findViewById(R.id.id_tab_my_img);

        mTextTrain = (TextView)findViewById(R.id.id_tab_train_text);
        mTextClub = (TextView)findViewById(R.id.id_tab_club_text);
        mTextPost = (TextView)findViewById(R.id.id_tab_post_text);
        mTextMy = (TextView)findViewById(R.id.id_tab_my_text);
    }

    @Override
    public void onClick(View v) {
        resetImgs();
        switch (v.getId())
        {
            case R.id.id_tab_train:
                setSelect(0);
                break;
            case R.id.id_tab_club:
                setSelect(1);
                break;
            case R.id.id_tab_hot:
                setSelect(2);
                break;
            case R.id.id_tab_post:
                setSelect(3);
                break;
            case R.id.id_tab_my:
                setSelect(4);
                break;

            default:
                break;
        }
    }

    private void resetImgs() {
        mImgTrain.setImageResource(R.drawable.ic_train1);
        mImgClub.setImageResource(R.drawable.ic_club1);
        mImgHot.setImageResource(R.drawable.ic_hot1);
        mImgPost.setImageResource(R.drawable.ic_post1);
        mImgMy.setImageResource(R.drawable.ic_my1);

        mTextTrain.setTextColor(Color.parseColor("#C8C8C8"));
        mTextClub.setTextColor(Color.parseColor("#C8C8C8"));
        mTextPost.setTextColor(Color.parseColor("#C8C8C8"));
        mTextMy.setTextColor(Color.parseColor("#C8C8C8"));
    }
}
